import path from 'node:path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import dts from 'vite-plugin-dts'


// https://vite.dev/config/
export default defineConfig(({ command }) => {
  const isBuild = command === 'build'
  return {
    root: isBuild ? undefined : path.resolve(__dirname, 'play'),
    plugins: [
      react(),
      dts({
        include: ['packages/components/**/*.ts', 'packages/components/**/*.tsx'],
        outDir: path.resolve(__dirname, 'dist'),
        insertTypesEntry: true, //这是dts插件的配置，用于生成类型文件
        rollupTypes: true,
        tsconfigPath: './tsconfig.app.json' // 库模式下需要指定tsconfig.app.json, 否则会报错
      })],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './play/src'),
        '#': path.resolve(__dirname, './packages')
      }
    },
    server: {
      host: true,
      port: 8000
    },
    build: {
      // 打包后文件目录
      outDir: path.resolve(__dirname, 'dist'),
      // 打包的配置（库模式）
      lib: {
        // 入口文件
        entry: path.resolve(__dirname, 'packages/components/index.ts'),
        name: 'ui',
        formats: ['es', 'cjs', 'umd', 'iife'],
        fileName: (format) => `ui.${format}.js`
      },
      rollupOptions: {
        external: ['react', 'react-dom'],
        output: {
          globals: {
            react: 'React',
            'react-dom': 'ReactDOM'
          }
        }
      }
    }
  }
})
